package com.wlys.player.controler.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.wlys.player.WlysApplication;
import com.wlys.player.controler.player.engine.PlayerEngine;
import com.wlys.player.controler.player.interf.IPlayerEngine;
import com.wlys.player.model.Track;

public class PlayerService extends Service implements IPlayerEngine
{

    IPlayerEngine playerEngine;
    private NotificationManager mNotificationManager;
    private static final int PLAYING_NOTIFY_ID = 667667;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        playerEngine = new PlayerEngine(this);
//        WlysApplication.getInstrance().setPlayerEngine(playerEngine);
        mNotificationManager = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        super.onStart(intent, startId);
        if (null == playerEngine)
        {
            return;
        }
        String action = intent.getStringExtra("action");
        if (PRE_ACTION.equals(action))
        {
            Track track = (Track) intent.getSerializableExtra("track");
            pre(track);
            return;
        }
        if (NEXT_ACTION.equals(action))
        {
            Track track = (Track) intent.getSerializableExtra("track");
            next(track);
            return;
        }
        if (PLAY_ACTION.equals(action))
        {
            Track track = (Track) intent.getSerializableExtra("track");
            play(track);
            return;
        }
        if (CONTINUE_ACTION.equals(action))
        {
            contin();
            return;
        }
        if (PAUSE_ACTION.equals(action))
        {
            pause();
            return;
        }
        if (STOP_ACTION.equals(action))
        {
            stop();
            return;
        }
        if (SEEK_TO_ACTION.equals(action))
        {
            int time = intent.getIntExtra("time", 0);
            seekTo(time);
            return;
        }
    }

    @Override
    public void pre(Track track)
    {
        playerEngine.pre(track);
    }

    @Override
    public void next(Track track)
    {
        playerEngine.next(track);
    }

    @Override
    public void play(Track track)
    {
        playerEngine.play(track);
    }

    @Override
    public void pause()
    {
        playerEngine.pause();
    }

    @Override
    public void stop()
    {
        playerEngine.stop();
    }

    @Override
    public void seekTo(int time)
    {
        playerEngine.seekTo(time);
    }

    @Override
    public void contin()
    {
        playerEngine.contin();
    }

    @Override
    public boolean isPlaying()
    {
        return playerEngine.isPlaying();
    }

//    private void displayNotifcation(PlaylistEntry playlistEntry)
//    {
//
//        String notificationMessage = playlistEntry.getTrack().getName() + " - " + playlistEntry.getAlbum().getArtistName();
//
//        Notification notification = new Notification(
//                R.drawable.stat_notify, notificationMessage, System.currentTimeMillis() );
//
//        PendingIntent contentIntent = PendingIntent.getActivity( this, 0,
//                new Intent( this, PlayerActivity.class ), 0);
//
//        notification.setLatestEventInfo( this, "Jamendo Player",
//                notificationMessage, contentIntent );
//        notification.flags |= Notification.FLAG_ONGOING_EVENT;
//
//        mNotificationManager.notify( PLAYING_NOTIFY_ID, notification );
//    }
}
